import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'
Vue.use(VueRouter)

// 引入路由
const Home = () => import('@/views/home/Home')
const Category = () => import('@/views/category/Category')
const Car = () => import('@/views/car/Car')
const User = () => import('@/views/user/User')
const Login = () => import('@/views/login/Login')
const Detail = () => import('@/views/detail/Detail')
const Product = () => import('@/views/product/Product')
const Register = () => import('../views/register/register')
const Profile = () => import('../views/profile/profile')
const routes = [{
    path: '/',
    redirect: '/home',
    meta: {
      index: 1
    }
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    meta: {
      index: 1,
    }
  },
  {
    path: '/car',
    name: 'Car',
    component: Car,
    meta: {
      index: 1,
      auth: true
    }
  }, {
    path: '/category',
    name: 'Category',
    component: Category,
    meta: {
      index: 1,
    }
  }, {
    path: '/user',
    name: 'User',
    component: User,
    meta: {
      index: 1,
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      index: 2,
    }
  },
  {
    path: '/detail/:id',
    name: 'Detail',
    component: Detail,
    meta: {
      index: 3,
    }
  },
  {
    path: '/product',
    name: 'Product',
    component: Product,
    meta: {
      index: 2,
    }
  },
  {
    path: '/register',
    name: Register,
    component: Register,
    meta:{
      index:2,
    }
  },
  {
    path: '/profile',
    name: Profile,
    component: Profile,
    meta:{
      index:2,
    }
  }
]
const router = new VueRouter({
  mode: 'history',
  routes
})

router.beforeEach((to, from, next) => { //校验是否登录
  // console.log(to);
  // console.log(to.meta.auth);
  if (to.meta.auth) {
    if (store.getters.getisLogin) {
      // console.log('true',store.getters.getisLogin);
      next()
    } else {
      next({
        path: '/login'
      })
      // console.log('false',store.getters.getisLogin);
    }
  } else {
    next()
  }
})

export default router